﻿Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.ComponentModel

Partial Public Class Loading
  Implements INotifyPropertyChanged

  Private WithEvents _timer As Timers.Timer
  Private iSeconds As Integer
  Private startTime As DateTime
  Public tsElapsedTime As TimeSpan

  Public ReadOnly Property ElapsedTime() As String
    Get
      Return String.Format("Elapsed time: {0} seconds", iSeconds)
    End Get
  End Property

  Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
  Protected Sub NotifyPropertyChanged(ByVal propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
  End Sub

  Public Sub New()
    MyBase.New()

    Me.InitializeComponent()

    ' Insert code required on object creation below this point.
    LayoutRoot.DataContext = Me
  End Sub

  Public Sub StartLoading()
    LoadingBar.Value = 0
    lblLoading.Content = "Loading Data..."

    startTime = DateTime.Now    
    iSeconds = 0
    NotifyPropertyChanged("ElapsedTime")

    _timer = New Timers.Timer(1000)
    _timer.Start()
  End Sub

  Public Sub StopLoading()
    If _timer IsNot Nothing Then
      _timer.Stop()
    End If

    tsElapsedTime = DateTime.Now.Subtract(startTime)
  End Sub

  Public Sub TimerInterval() Handles _timer.Elapsed
    iSeconds += 1
    NotifyPropertyChanged("ElapsedTime")
  End Sub
End Class
